#!/bin/sh
root_dir=`pwd`
backupDir=backup_config
mkdir  ${backupDir}
time_str=`date  +%Y%m%d%H%M%S`

input_file=$1

header_auth="Authorization:Basic dmNvbV9vbmxpbmV1cGRhdGU6VmNvbUAyMDE5"

fileline=`cat ${input_file}|grep -v  "#"`
echo "fileline---->"${fileline}

file_num=`cat ${input_file}|grep -v "#"|grep -v '^$'|wc -l`
echo "file_num="${file_num}
if [ ${file_num} != 1 ]
then 
        echo_error  "error: ${input_file}, file num is not one."
        exit;
fi

#VerifyServices----http://120.194.3.112:1000/ddc_update/VerifyServices.tgz----/home/IOTPlatform/----init.conf----/home/IOTPlatform/stopVerifyServices.sh----/home/IOTPlatform/startVerifyServices.sh

app_name=`echo ${fileline} | awk -F"----" '{print $1}'`
app_tgz_url=`echo ${fileline} | awk -F"----" '{print $2}'`
app_deploy_dir=`echo ${fileline} | awk -F"----" '{print $3}'`
app_proc_config=`echo ${fileline} | awk -F"----" '{print $4}'`
app_proc_stop=`echo ${fileline} | awk -F"----" '{print $5}'`
app_proc_start=`echo ${fileline} | awk -F"----" '{print $6}'`

if [ "/" == ${app_deploy_dir: -1} ]
then
        app_deploy_dir=${app_deploy_dir%?}
fi


echo -e  "---->${git_proc_name}--${git_code_url}--${git_deploy_dir}--${git_proc_config}--${git_proc_stop}--${git_proc_start}"

echo -e  "start ................................................................."



#1.Update Code From  http_url
app_tgz_name=${app_name}_${time_str}.tgz
rm -fr ${app_tgz_name}  ${app_name}
wget --header "${header_auth}"  ${app_tgz_url}  -O  ${app_tgz_name}
tar zxf ${app_tgz_name}
mkdir ${app_deploy_dir}  -p


#2.backup Config
app_proc_config_num=`echo ${app_proc_config}|awk -F"," '{print NF}'`
back_config_dir="${app_deploy_dir}/${app_name}"
back_config_backdir="${backupDir}/${app_name}_${time_str}"
mkdir   ${back_config_backdir}  -p


echo "----------------------------------------------------app_proc_config_num=${app_proc_config_num}"
if [ ${app_proc_config_num} -le 1 ]
then
        app_proc_config_1=`echo ${app_proc_config}|awk -F"," '{print $1}'`
        back_config_file_1="${back_config_dir}/${app_proc_config_1}"
        back_config_backfile_1="${back_config_backdir}/${app_proc_config_1}"
        mkdir `dirname  ${back_config_backfile_1} `  -p
        back_config_dir_1=`dirname  ${back_config_file_1} `

        echo "cp -fr  ${back_config_file_1}    ${back_config_backfile_1}"
        cp -fr  ${back_config_file_1}    ${back_config_backdir}
        \cp -fr  ${back_config_file_1}    ${back_config_backfile_1}

else
        back_config_file="${back_config_dir}/${app_proc_config}"
        back_config_backfile="${back_config_backdir}/${app_proc_config}"

        app_proc_config_1=`echo ${app_proc_config}|awk -F"," '{print $1}'`
        app_proc_config_2=`echo ${app_proc_config}|awk -F"," '{print $2}'`
        back_config_file_1="${back_config_dir}/${app_proc_config_1}"
        back_config_backfile_1="${back_config_backdir}/${app_proc_config_1}"
        back_config_dir_1=`dirname  ${back_config_file_1} `
        mkdir `dirname  ${back_config_backfile_1} `  -p

        back_config_file_2="${back_config_dir}/${app_proc_config_2}"
        back_config_backfile_2="${back_config_backdir}/${app_proc_config_2}"
        back_config_dir_2=`dirname  ${back_config_file_2} `

        echo "cp -fr  ${back_config_file_1}    ${back_config_backdir}"
        echo "cp -fr  ${back_config_file_2}    ${back_config_backdir}"
        cp -fr  ${back_config_file_1}    ${back_config_backdir}
        cp -fr  ${back_config_file_2}    ${back_config_backdir}
        \cp -fr  ${back_config_file_1}    ${back_config_backfile_1}
        \cp -fr  ${back_config_file_2}    ${back_config_backfile_2}

fi

#3.stop Proc
${app_proc_stop}

#4.Recover Code From RootDir
mv  ${app_deploy_dir}/${app_name}       ${app_deploy_dir}/${app_name}_${time_str}
cp  -fr ${app_name}  ${app_deploy_dir} 

#5.recover Config From Backup Dir
if [ ${app_proc_config_num} -le 1 ]
then
        echo "cp -fr  ${back_config_backfile_1}    ${back_config_dir_1}"
        cp -fr  ${back_config_backfile_1}    ${back_config_dir_1}
else
        echo "cp -fr  ${back_config_backfile_1}    ${back_config_dir_1}"
        echo "cp -fr  ${back_config_backfile_2}    ${back_config_dir_2}"
        cp -fr  ${back_config_backfile_1}    ${back_config_dir_1}
        cp -fr  ${back_config_backfile_2}    ${back_config_dir_2}
fi



#6.Start Proc
${app_proc_start}

echo  -e  "Proc  Update is success !!!... "